/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Text;
using EPiServer.Core;
using EPiServer.Web;

namespace EPiServer.Municipality.Extensions {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    public static class PageDataExtensions {
        /* *******************************************************************
		*  Properties
		* *******************************************************************/
        /* *******************************************************************
		*  Constructors
		* *******************************************************************/
        /* *******************************************************************
		*  Methods
		* *******************************************************************/
        #region public static bool IsValue(this PageData page, string propertyName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="propertyName"></param>
        /// <returns>True if it is value, otherwise false.</returns>
        public static bool IsValue(this PageData page, string propertyName) {
            if (page.PageLink == null || page.PageLink.ID < 1) {
                return false;
            }

            var propertyData = page.Property[propertyName];

            return (propertyData != null && !propertyData.IsNull);
        }
        #endregion
        #region public static string GetExternalUrl(this PageData page)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string GetExternalUrl(this PageData page) {
            var result = string.Empty;

            if (page.PageLink != null && page.PageLink.ID > 0) {
                var url = new UrlBuilder(UriSupport.Combine(EPiServer.Configuration.Settings.Instance.SiteUrl.ToString(), page.LinkURL));
                if (UrlRewriteProvider.IsFurlEnabled) {
                    EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, page, Encoding.UTF8);
                }

                result = url.ToString();
            }

            return result;
        }
        #endregion
        #region public static T PropertyValue<T>(this PageData page, string propertyName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static T PropertyValue<T>(this PageData page, string propertyName) {
            return PropertyValueWithDefault<T>(page, propertyName, default(T));
        }
        #endregion
        #region public static T PropertyValue<T>(this PageData page, string propertyName, string defaultValue)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="propertyName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T PropertyValue<T>(this PageData page, string propertyName, string defaultValue) where T : PropertyData, new() {
            T prop=new T();
            prop.ParseToSelf(defaultValue);
            return PropertyValueWithDefault<T>(page, propertyName, prop);
        }
        #endregion
        #region public static T PropertyValueWithDefault<T>(this PageData page, string propertyName, T defaultValue)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="propertyName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T PropertyValueWithDefault<T>(this PageData page, string propertyName, T defaultValue) {
            if (page.IsValue(propertyName)) {
                return (T)page.GetValue(propertyName);
            }

            return defaultValue;
        }
        #endregion
        /* *******************************************************************
		*  Event methods
		* *******************************************************************/
        
    }
}